const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let n, d;
let strengthArr = [];
rl.on("line", (line)=>{
    if(n === undefined) {
        [n, d] = line.trim().split(" ").map(Number)
    } else {
        strengthArr = line.trim().split(" ").map(Number)
        console.log(getResult(n, d, strengthArr));
        // clear
        n = d = undefined
        strengthArr.length = 1
    }

})

/**
 *  返回组队最小实力差
 * @param n {number} 队伍数
 * @param d {number} 实力差距
 * @param strengthArr  {[number]}初始队伍实力数组
 */
function getResult(n, d, strengthArr) {
    //动态规划, 表示角标到i对时, 组成的组合对应的实力总差值
    let dp = new Array(n + 1).fill(null).map(_ => new Array(Math.floor(n/2) + 1).fill(Infinity))
    dp[0][0] = 0;
    strengthArr.sort((a, b)=> a - b)

    for (let i = 1; i <= n; i++) { // 队伍
        for (let j = 0; j <= n/2 ; j++) { // 组合
            dp[i][j] = dp[i - 1][j] // 不用当前队

            // 用当前及之前队
            if(j > 0 && i >=2) {
                const diff = Math.abs(strengthArr[i - 1] - strengthArr[i - 2]) // 注意这里i= 1时, 表示第一个队伍
                if(diff <= d) {
                    dp[i][j] = Math.min(dp[i][j], dp[i - 2][j - 1] + diff) // 不要当前队 & 要当前队
                }
            }
        }
    }

    // 队伍遍历(倒序, 先考虑最多队伍)
    for (let j = Math.floor(n/2); j >= 0 ; j--) {
        if(dp[n][j] !== Infinity) {
            return dp[n][j] || -1
        }
    }
}